/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "kh_sle_c_interface_test_entry.h"
#include "kh_sle_announce.h"
#include "kh_sle_host.h"
#include "sle_error_code.h"
#include "sle_unit_test_log.h"
#include "stdlib.h"

SleAnnounceCallbacks *g_sleAnnounceUtCbs = NULL;

void OnSleUtAnnounceStateChangeCallback(uint8_t announceId, SleAnnounceState announceState, bool isTerminaled)
{
    return;
}

void InitSleAnnounceServiceTestLevel0(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = InitSleAnnounceService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]InitSleAnnounceServiceTestLevel0 failed(%d)", ret);
}

void RegisterAnnounceCallbackTestLevel0(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = RegisterAnnounceCallbacks(g_sleAnnounceUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]RegisterAnnounceCallbackTestLevel0 failed(%d)", ret);
}

static uint8_t g_announceId;
static SleAnnounceData g_data = {
    .announceLength = 10,
    .announceData = "1234567890"
};
void AddAnnounceTestLevel0(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = AddAnnounce(&g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]AddAnnounceTestLevel0 failed(%d)", ret);
}

void StartAnnounceTestLevel0(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = StartAnnounce(g_announceId, &g_data, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]StartAnnounceTestLevel0 failed(%d)", ret);
}

void StopAnnounceTestLevel0(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = StopAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]StopAnnounceTestLevel0 failed(%d)", ret);
}

void RemoveAnnounceTestLevel0(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = RemoveAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]RemoveAnnounceTestLevel0 failed(%d)", ret);
}

void UnregisterAnnounceCallbacksTestLevel0(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = UnregisterAnnounceCallbacks(g_sleAnnounceUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]UnregisterAnnounceCallbacksTestLevel0 failed(%d)", ret);
}

void DeinitSleAnnounceServiceTestLevel0(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = DeinitSleAnnounceService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]DeinitSleAnnounceServiceTestLevel0 failed(%d)", ret);
}

void KhSleAnnounceTestLevel0(void)
{
    SLE_UT_LOG("___KhSleAnnounceTestLevel0 start");
    InitSleAnnounceServiceTestLevel0();
    RegisterAnnounceCallbackTestLevel0();
    AddAnnounceTestLevel0();
    StartAnnounceTestLevel0();
    StopAnnounceTestLevel0();
    RemoveAnnounceTestLevel0();
    UnregisterAnnounceCallbacksTestLevel0();
    DeinitSleAnnounceServiceTestLevel0();
    SLE_UT_LOG("___KhSleAnnounceTestLevel0 end");
}

void InitSleAnnounceServiceTestLevel1(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = InitSleAnnounceService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]InitSleAnnounceServiceTestLevel1 failed1(%d)", ret);
    //测试重复调用
    ret = InitSleAnnounceService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]InitSleAnnounceServiceTestLevel1 failed2(%d)", ret);
}

void RegisterAnnounceCallbackTestLevel1(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    //测试入参为NULL
    ret = RegisterAnnounceCallbacks(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleHostTest][FAILED]RegisterAnnounceCallbackTestLevel1 failed1(%d)", ret);
    //测试重复调用
    ret = RegisterAnnounceCallbacks(g_sleAnnounceUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]RegisterAnnounceCallbackTestLevel1 failed2(%d)", ret);
    ret = RegisterAnnounceCallbacks(g_sleAnnounceUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]RegisterAnnounceCallbackTestLevel1 failed3(%d)", ret);
}

void AddAnnounceTestLevel1(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    //测试入参为NULL
    ret = AddAnnounce(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleHostTest][FAILED]AddAnnounceTestLevel1 failed1(%d)", ret);
    //测试重复调用
    uint8_t announceId;
    for(int i = 0; i < SLE_MAX_ANNOUNCE_ID_NUM; i++){
        ret = AddAnnounce(&announceId);
        SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]AddAnnounceTestLevel1 failed2(%d)", ret);
    }
    ret = AddAnnounce(&announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_OUT_RANGE, "[KhSleHostTest][FAILED]AddAnnounceTestLevel1 failed3(%d)", ret);
    for (size_t i = 0; i < SLE_MAX_ANNOUNCE_ID_NUM; i++) {
        ret = RemoveAnnounce(i);
        SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]AddAnnounceTestLevel1 failed4(%d)", ret);
    }
    AddAnnounceTestLevel0();
}

void StartAnnounceTestLevel1(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    //测试入参为未注册的announceId
    ret = StartAnnounce(g_announceId + 1, &g_data, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_STATUS_WRONG, "[KhSleHostTest][FAILED]StartAnnounceTestLevel1 failed1(%d)", ret);
    //测试入参为NULL
    ret = StartAnnounce(g_announceId, NULL, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleHostTest][FAILED]StartAnnounceTestLevel1 failed2(%d)", ret);
    //测试重复调用
    ret = StartAnnounce(g_announceId, &g_data, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]StartAnnounceTestLevel1 failed3(%d)", ret);
    ret = StartAnnounce(g_announceId, &g_data, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_EBABLE_ANNOUNCE_SUCCESS_WHEN_ANNOUNCE, "[KhSleHostTest][FAILED]StartAnnounceTestLevel1 failed4(%d)", ret);
}

void StopAnnounceTestLevel1(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    //测试入参为未注册的announceId
    ret = StopAnnounce(g_announceId + 1);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_STATUS_WRONG, "[KhSleHostTest][FAILED]StopAnnounceTestLevel1 failed1(%d)", ret);
    //测试重复调用
    ret = StopAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]StopAnnounceTestLevel1 failed2(%d)", ret);
    ret = StopAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_DISABLE_ANNOUNCE_SUCCESS_WHEN_NOT_ANNOUNCE, "[KhSleHostTest][FAILED]StopAnnounceTestLevel1 failed3(%d)", ret);
}

void RemoveAnnounceTestLevel1(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    //测试入参为未注册的announceId
    ret = RemoveAnnounce(g_announceId + 1);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_STATUS_WRONG, "[KhSleHostTest][FAILED]RemoveAnnounceTestLevel1 failed1(%d)", ret);
    //测试入参为正在广播中的announceId
    StartAnnounceTestLevel0();
    ret = RemoveAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_STATUS_WRONG, "[KhSleHostTest][FAILED]RemoveAnnounceTestLevel1 failed2(%d)", ret);
    StopAnnounceTestLevel0();
    //测试重复调用
    ret = RemoveAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]RemoveAnnounceTestLevel1 failed3(%d)", ret);
    ret = RemoveAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]RemoveAnnounceTestLevel1 failed4(%d)", ret);
}

void UnregisterAnnounceCallbacksTestLevel1(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    //测试入参为NULL
    ret = UnregisterAnnounceCallbacks(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_INVALID_PARAM, "[KhSleHostTest][FAILED]UnregisterAnnounceCallbacksTestLevel1 failed1(%d)", ret);
    //测试重复调用
    ret = UnregisterAnnounceCallbacks(g_sleAnnounceUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]UnregisterAnnounceCallbacksTestLevel0 failed(%d)", ret);
    ret = UnregisterAnnounceCallbacks(g_sleAnnounceUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]UnregisterAnnounceCallbacksTestLevel1 failed2(%d)", ret);
}

void DeinitSleAnnounceServiceTestLevel1(void)
{
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = DeinitSleAnnounceService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_NO_ERROR, "[KhSleHostTest][FAILED]DeinitSleAnnounceServiceTestLevel1 failed1(%d)", ret);
    //测试重复调用
    ret = DeinitSleAnnounceService();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_ERR_NOT_READY, "[KhSleHostTest][FAILED]DeinitSleAnnounceServiceTestLevel1 failed2(%d)", ret);
}

void KhSleAnnounceTestLevel1(void)
{
    SLE_UT_LOG("___KhSleAnnounceTestLevel1 start");
    InitSleAnnounceServiceTestLevel1();
    RegisterAnnounceCallbackTestLevel1();
    AddAnnounceTestLevel1();
    StartAnnounceTestLevel1();
    StopAnnounceTestLevel1();
    RemoveAnnounceTestLevel1();
    UnregisterAnnounceCallbacksTestLevel1();
    DeinitSleAnnounceServiceTestLevel1();
    SLE_UT_LOG("___KhSleAnnounceTestLevel1 end");
}

SleErrorCode InitKhSleAnnounceTestEntry(void)
{
    g_sleAnnounceUtCbs = (SleAnnounceCallbacks *)malloc(sizeof(SleAnnounceCallbacks));
    g_sleAnnounceUtCbs->OnSleAnnounceStateChangeCb = OnSleUtAnnounceStateChangeCallback;
    return SLE_NO_ERROR;
}

SleErrorCode DeinitKhSleAnnounceTestEntry(void)
{
    free(g_sleAnnounceUtCbs);
    return SLE_NO_ERROR;
}

void KhSleAnnounceTestEntry(void)
{
    SLE_UT_LOG("__KhSleAnnounceTestEntry start");
    SleErrorCode ret = SLE_ERR_UNKNOWN;
    ret = InitKhSleAnnounceTestEntry();
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_NO_ERROR, "[KhSleAnnounceTest][FAILED]InitKhSleAnnounceTestEntry failed(%d)", ret);
    KhSleAnnounceTestLevel0();
    KhSleAnnounceTestLevel1();
    ret = DeinitKhSleAnnounceTestEntry();
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_NO_ERROR, "[KhSleAnnounceTest][FAILED]DeinitKhSleAnnounceTestEntry failed(%d)", ret);
    SLE_UT_LOG("__KhSleAnnounceTestEntry end");
}

